<template>
  <!-- 经销商施工进度维护 -->
  <div>
    <el-row >
    <el-col :span="13" >
      
      <BreakSpace label="项目搜索区" :containerStyle="{ marginBottom: '10px' }" />
      <!-- 搜索 -->
      <TopFilter  
        :list="topFilterInit"  
        :cols="1"  
        labelWidth="70"  
        :collapse=false
        @filterChange="changeHandle"
        @onCollapse="collapseHandle"
        ></TopFilter>
      <!-- table -->
      <FilterTable   
          ref="table" 
          height="350px"
          :columns="columns"   
          :fetchapi="fetchApi" 
          :isSelectColumn="isSelectColumn"
          selectionType="single"
          :params="fetchParams" 
          :datakey="'records'"
          columnsRef='origin_construct_process_dealerFeedbackAdd_01'
          :defaultSelections="selections"
          :onSyncTableData="syncDataHandle"
          :onRowSelectChange="selectChange"
          :onColumnsChange="columns => this.columns = columns">
      </FilterTable>
    </el-col>
    <el-col :span="10" :offset="1">
      <BreakSpace label="附件上传区" :containerStyle="{ marginBottom: '10px' }" />
      <!-- 附件表单 -->
      <FormPanel
        ref="fileInfo"
        :list="initFormFile"
        :cols="1"
        :labelWidth="80"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
     </el-col>
    </el-row>
    <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">确定</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { searchConstruction } from '@/api/cms-api/origin/baseSetting/constructionChilren';
import { insertDealerApplyDealerFeedback,updataDealerApplyDealerFeedback} from '@/api/cms-api/origin/construct/dealer';
export default {
  components: {
  },
  mixins: [dictionary],
  props: {
    value:{
        type: Object,
        default: null
    }
  },
  data() {
    return {
      formData:[],
      visible:false,
      isSelectColumn:true,
      infoId:null,
      // constructionId:[],
      topFilterInit:this.topFilterInitFun(),
      fetchParams:{"dealerType":this.value.dealerType,"status":10031001,"applyId":this.value.applyId,"constructionIdCurrent":this.value.rowData?this.value.rowData.constructionId:""},  //参数
      initData: {},
      selections :[],   //表格回显参数
      columns:this.creatHeader(),
      initFormFile: this.initFormFileFun(),
      fetchApi: searchConstruction,
      selectedRows: []//选中项目
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    //如果传入参数不为空，则说明是修改进入，初始化对应数据
    if(this.value!=null && (this.value.type=="edit"||this.value.type=="show")){
      this.init();
    }
  },
  methods: {
      //修改时，初始化页面数据
      init(){
        // 更新接口需要的记录 ID
        this.infoId = this.value.rowData.applyDealerFeedbackId;
        // this.constructionId.push(this.value.rowData.constructionId);
        console.log("this.value.rowData:",this.value.rowData);
        //循环为表单赋值
        this.initFormFile.forEach(x => {
          if(x.fieldName == "fileUrl"){ //如果为附件
            let urlsList = this.value.rowData.urls;
            if(urlsList!=null && urlsList.length>0){
              let fileList = [];
              for(let i in urlsList){
                  let fileName = { name: urlsList[i].fileName };
                  let fileId = { url: urlsList[i].fileId };
                  let f = Object.assign(fileName, fileId);
                  fileList.push(f);
              }
              x.initialValue = fileList;
            }
          }else{
            x.initialValue = this.value.rowData[x.fieldName];
          }
        });
      },
      
      syncDataHandle(dataList, isFirst) {
        if (!dataList.length) return;
        if(this.value!=null && (this.value.type=="edit"||this.value.type=="show")){
          for(let i in dataList){
            if(this.value.rowData.constructionId==dataList[i].constructionId){
                this.selections = [dataList[i]];
                return;
            }
          }
           
        }
      },
      //搜索数据
      topFilterInitFun(){
        return [
            { type: 'INPUT',   
              label: '项目名称',    
              fieldName: 'constructionNamePra',  
              // initialValue: '',  
              placeholder: '请输入'
            },
            { type: 'INPUT',   
              label: '子项目名称',    
              fieldName: 'constructionName',  
              // initialValue: '',  
              placeholder: '请输入'
            }
        ]
      },
      //搜索获取数据
      changeHandle(val){
        // let constructionIdCurrent = "";
        // if(this.value!=null && (this.value.type=="edit"||this.value.type=="show")){
        //   constructionIdCurrent = this.value.rowData.constructionId;
        //    
        // }
        this.fetchParams = Object.assign({"dealerType":this.value.dealerType,
          "status":10031001,"applyId":this.value.applyId,"constructionIdCurrent":this.value.rowData?this.value.rowData.constructionId:""},val);
      },
      //table表头和数据源
      creatHeader(){
         return [
         {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
         },
        { title: '项目名称',  
          dataIndex: 'constructionNamePra', 
          showOverflowTooltip: true,
          sorter: true
        },
        { title: '子项目名称',  
          dataIndex: 'constructionName', 
          showOverflowTooltip: true,
          sorter: true
        }
        ]
      },
      //附件信息数据
    initFormFileFun() {
      return [
        { type: 'INPUT',   
          label: '项目名称',    
          fieldName: 'constructionNamePra',  
          // initialValue: '',  
          placeholder: '',
          disabled:true,
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        { type: 'INPUT',   
          label: '子项目名称',    
          fieldName: 'constructionName',  
          // initialValue: '',  
          disabled:true,
          placeholder: ''
        },
        {
          type: 'UPLOAD_FILE',
          label: '附件',
          fieldName: 'fileUrl',
          initialValue: '',
          selfCols:2,
          upload: {
            limit:0,
            actionUrl: '/api/file/oss/upload',
            fileTypes: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'pdf', 'rar', 'zip'],
            max:1024*30
          },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        }
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      if(this.selectedRows!=null&&this.selectedRows.length>0){

        this.initFormFile[0].initialValue = rows[0].constructionNamePra;  //显示项目名
        this.initFormFile[1].initialValue = rows[0].constructionName;     //显示子项目名
        this.infoId = rows[0].constructionId;       //存储项目id
      }else{
        this.initFormFile[0].initialValue = "";  //显示项目名
        this.initFormFile[1].initialValue = "";     //显示子项目名
        this.infoId = "";       //存储项目id
      }
    },
    formChangeHandle(data){
      this.formData = data;
       this.doSaveDialog();
    },
    //保存按钮
    saveDialog() {
      this.$refs.fileInfo.SUBMIT_FORM();
    },
    //执行保存
    async doSaveDialog() {
      let fileUrlList = [];
      //获取附件，转为list类型
      for (let i = 0; i < this.formData.fileUrl.length; i++) {
        fileUrlList.push(Object.assign({},{"name":this.formData.fileUrl[i].name},{"url":this.formData.fileUrl[i].url}));
      }

      let submitData = Object.assign({},{"constructionId":this.infoId,"auditResult":25501001},{"applyId":this.value.applyId});
      submitData.filesInfo = fileUrlList;
      let res;
      //如果传入参数不为空并且为修改
      if(this.value!=null && this.value.type=="edit"){
        res = await updataDealerApplyDealerFeedback(submitData,this.value.rowData["applyDealerFeedbackId"]);
      }else{
        res = await insertDealerApplyDealerFeedback(submitData);
      }
      if (res.resultCode === 200) {
        this.$notify.success({title: '提示信息',message:'保存成功!'});
        // 重新加载
        this.closeDialog('refresh');
      }else {
        this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
        this.closeDialog();
      }
      // this.$emit('close', { visible: false ,"data":this.selectedRows});
    },
    //取消操作
    closeDialog(type) {
      this.$emit('close', { visible: false,type:type});
    },
     collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    }
  }
};
</script>

<style lang="less" scoped>
</style>
